package com.liu.backstage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.backstage.model.Question;
import com.liu.backstage.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 腾杰说
 * @since 2020-07-08
 */
@Controller
@RequestMapping("/question")
@PreAuthorize("hasAnyRole('ROLE_USER')")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @RequestMapping("/findAll")
    public String findAll(){
        return "productlist";
    }

    @RequestMapping("/getData")
    public String getData(Model model,@RequestParam(value = "page" ,defaultValue = "1")Integer page, @RequestParam(value = "pageSize" ,defaultValue = "2")Integer pageSize){

        Page<Question> page1 = new Page<>(page, pageSize);
        IPage<Question> list = questionService.page(page1);
        model.addAttribute("questionList",list.getRecords());//数据
        model.addAttribute("cur",list.getCurrent());//当期页
        model.addAttribute("last",list.getPages());//最终页
        model.addAttribute("next",list.getCurrent()+1); //下一页
        model.addAttribute("pre",list.getCurrent()-1);//上一页
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("sum",list.getTotal());
        return "productlistData";
    }

    @RequestMapping("/deletequestion")
    public String deletequestion(@RequestParam(value = "id",required = true)Integer id){

        questionService.removeById(id);
        return "redirect:findAll";
    }

    @PostMapping("/deletelist")
    @ResponseBody
    public String deletelist(String cks){
        String[] split = cks.split(",");
        List<String> strings = Arrays.asList(split);
        for (String string : strings) {
            questionService.removeById(string);
        }
        return "true";
    }
}

